#include "CharacterEditForm.h"
#include "ui_CharacterEditForm.h"
#include "CharacterDataDelegate.h"

#include <D20CharacterItem.h>
#include <D20CampaignItem.h>
#include <FileHandler.h>
#include <ValueItem.h>


#include <QFileDialog>
#include <QSortFilterProxyModel>


CharacterEditFormBase::CharacterEditFormBase(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CharacterEditForm)
{
    m_campaign = 0;
    m_character = new D20CharacterModel(this);

    QSortFilterProxyModel* sortFilterProxy = new QSortFilterProxyModel(this);
    sortFilterProxy->setSourceModel(m_character);


    ui->setupUi(this);

    // add actions
    addAction(ui->actionDelete);

    // setup tree view
    ui->treeViewCharacterClass->setModel(sortFilterProxy);
    ui->treeViewCharacterClass->header()->setResizeMode(QHeaderView::ResizeToContents);
    ui->treeViewCharacterClass->setItemDelegate(new CharacterDataDelegate(m_character, this));

    connect(FileHandler::instance(), SIGNAL(sigFileOpened()), this, SLOT(onUpdateCampaigns()));
}

CharacterEditFormBase::~CharacterEditFormBase()
{
    delete ui;
}


void CharacterEditFormBase::updateData()
{
    on_lineEditName_editingFinished();
}



D20BaseModel* CharacterEditFormBase::model() const
{
    return m_character;
}


void CharacterEditFormBase::initialize()
{
    D20CharacterItem* characterRoot = qobject_cast<D20CharacterItem*>(root());
    if (!characterRoot) return;

    if (!m_character) m_character = new D20CharacterModel(this);

    m_character->setRootItem(characterRoot);

    ui->lineEditName->setText(characterRoot->name());

    openCampaign(characterRoot->campaignFile());

}


void CharacterEditFormBase::onActionTriggered(QAction *action)
{
    Q_UNUSED(action);
}


void CharacterEditFormBase::onUpdateCampaigns()
{
    ui->comboBoxCampaign->blockSignals(true);
    foreach (RootItem* rootItem, FileHandler::openItems())
    {
        D20CampaignItem* campaignItem = qobject_cast<D20CampaignItem*>(rootItem);
        if (!campaignItem) continue;

        QString name = campaignItem->name();
        if (name.isEmpty()) name = tr("unnamed campaign");

        QString file_path = campaignItem->filePath();

        QString text = QString("%1 (%2)").arg(name).arg(file_path);

        int index = ui->comboBoxCampaign->findData(file_path);
        if (index >= 0)
        {
            ui->comboBoxCampaign->setItemText(index, text);
            continue;
        }

        ui->comboBoxCampaign->addItem(text, file_path);
    }

    ui->comboBoxCampaign->blockSignals(false);

    if (!m_campaign) return;

    ui->comboBoxCampaign->blockSignals(true);
    int index = ui->comboBoxCampaign->findData(m_campaign->rootItem()->filePath());
    if (index >= 0) ui->comboBoxCampaign->setCurrentIndex(index);
    ui->comboBoxCampaign->blockSignals(false);
}


void CharacterEditFormBase::openCampaign(const QString& file_path)
{
    // delete old campaign model
    if (m_campaign) m_campaign->deleteLater();

    // get campaign root
    RootItem* campaignRoot = FileHandler::open(file_path);
    if (!campaignRoot) return;

    m_campaign = new D20CampaignModel(this);
    m_campaign->setRootItem(campaignRoot);
    m_character->setCampaignModel(m_campaign);

    ui->treeViewCharacterClass->sortByColumn(AbstractItem::NameData, Qt::AscendingOrder);
}


void CharacterEditFormBase::on_pushButtonOpenCampaign_clicked()
{
    QString file_path = QFileDialog::getOpenFileName(this, tr("Open Campaign..."), QDir::currentPath(), tr("Xml File (*.xml)"));
    if (file_path.isEmpty()) return;

    openCampaign(file_path);
}


void CharacterEditFormBase::on_comboBoxCampaign_currentIndexChanged(int index)
{
    QString file_path = ui->comboBoxCampaign->itemData(index).toString();
    if (file_path.isEmpty() || m_campaign->rootItem()->filePath() == file_path) return;

    openCampaign(file_path);
}


void CharacterEditFormBase::on_lineEditName_editingFinished()
{
    QString name = ui->lineEditName->text();
    if (name.isEmpty()) return;

    m_character->rootItem()->setData(AbstractItem::NameData, name);
}



void CharacterEditFormBase::on_actionDelete_triggered()
{
    QAbstractItemModel* model = ui->treeViewCharacterClass->model();
    if (!model) return;

    QModelIndex index = ui->treeViewCharacterClass->currentIndex();
    if (!index.isValid()) return;

    model->removeRows(index.row(), 1, index.parent());
}
